<?php
/**
 * 夏日葵电商系统
 * http://www.xiarikui.com
 */
namespace Admin\Controller;
use Think\AjaxPage;
use Think\Page;
class BusinessController extends BaseController {
    public function index() {
        $this->display();
    }
    /**
     * 会员列表
     */
    public function ajaxIndex() {
        $condition = array();
        I('phone') ? $condition['phone'] = I('phone') : false;
        I('store_name') ? $condition['store_name'] = I('store_name ') : false;
        $sort_order = 'id desc';
        $model = M('business');
        $count = $model->where($condition)->count();
        $Page = new AjaxPage($count, 10);
        foreach ($condition as $key => $val) {
            $Page->parameter[$key] = urlencode($val);
        }
        $userList = $model->where($condition)->order($sort_order)->limit($Page->firstRow . ',' . $Page->listRows)->select();
        $show = $Page->show();
        $this->assign('userList', $userList);
        $this->assign('page', $show);
        $this->display();
    }
    /**
     * 会员详细信息查看
     */
    public function detail() {
        $uid = I('get.id');
        $user = D('business')->where(array('id' => $uid))->find();
        if (!$user)
            exit($this->error('商家不存在'));
        if (IS_POST) {
			$check = M('users')->where(array('id'=>array('neq',$uid),'phone'=>$_POST['phone']))->count();
			if($check){
				exit($this->error('该商家已存在'));
			}
            //  会员信息编辑
            $password = I('post.password');
            $password2 = I('post.password2');
            if ($password != '' && $password != $password2) {
                exit($this->error('两次输入密码不同'));
            }
            if ($password == '' && $password2 == '') {
                unset($_POST['password']);
            } else {
                $_POST['password'] = encrypt($_POST['password']);
            }
            if($_FILES['store_img']['name']){
                $_POST['store_img'] = '/Public/upload/business/'.$this->_uploadOne('store_img');
            }
            $row = M('business')->where(array('id' => $uid))->save($_POST);
            if ($row)
                exit($this->success('修改成功'));
            exit($this->error('未作内容修改或修改失败'));
        }
        $this->assign('user', $user);
        $this->display();
    }
    public function addBusiness() {
        if (IS_POST) {
            $data = I('post.');
			if (!$data['store_name']) {
                exit($this->error('请输入商家名称'));
            }
            $mod = M('business');
            if ($mod->where(array('phone'=>$data['phone']))->count() > 0) {
                return array('status' => -1, 'msg' => '账号已存在');
            }
            if($_FILES['store_img']['name']){
                $data['store_img'] = '/Public/upload/business/'.$this->_uploadOne('store_img');
            }
            $data['password'] = encrypt($data['password']);
            $data['add_time'] = time();
            $user_id = $mod->add($data);
            if ($user_id) {
                $goods_list = M('goods')->field('id')->select();
                $spec_mod = M('goods_spec');
                $store_mod = M('goods_store');
                $store_spec_mod = M('goods_store_spec');
                foreach((array)$goods_list as $k=>$v){
                    $store_mod->add(array('store_id'=>$user_id,'goods_id'=>$v['id'],'add_time'=>time()));
                    $spec_list = $spec_mod->where(array('goods_id'=>$v['id']))->select();
                    foreach((array)$spec_list as $kk=>$vv){
                        $store_spec_mod->add(array('store_id'=>$user_id,'goods_id'=>$v['id'],'spec_id'=>$vv['id'],'spec_name'=>$vv['spec_name'],'spec_price'=>$vv['spec_price'],'spec_mk_price'=>$vv['spec_mk_price']));
                    }
                }
                $this->success('添加成功', U('Business/index'));
                exit;
            } else {
                $this->error('添加失败,', U('Business/index'));
            }
        }
        $this->display();
    }
    /**
     * 用户收货地址查看
     */
    public function address() {
        $uid = I('get.id');
        $lists = D('user_addr')->where(array('user_id' => $uid))->select();
        $regionList = M('Region')->getField('id,name');
        $this->assign('regionList', $regionList);
        $this->assign('lists', $lists);
        $this->display();
    }
	
	//提现列表
	function withdraw(){
		$mod = M('business_withdraw');
		if(i('act')=='ajax'){
			$id = I('id')?I('id'):'';
			$withdraw = $mod->where(array('id'=>$id))->find();
			if(!$withdraw['status']){
				$result = $mod->where(array('id'=>$id))->save(array('status'=>1,'up_time'=>time()));
				if($result){
					$b_mod = M('business');
					$withdraw_price = $b_mod->where(array('id'=>$withdraw['store_id']))->getField('withdraw_price');
					$b_mod->where(array('id'=>$withdraw['store_id']))->save(array('withdraw_price'=>$withdraw_price-$withdraw['price']));
				}
				exit(json_encode(array('code'=>200,'msg'=>'提现成功')));
			}
		}
        $p = empty($_REQUEST['p']) ? 1 : $_REQUEST['p'];
        $size = 20;
        $count = $mod->where('1=1')->count();
        $pager = new \Think\Page($count, $size);
        $page = $pager->show();
        $withdraw = $mod->alias('as a')
			->join('left join '.C('DB_PREFIX').'business as c on c.id=a.store_id')
			->where('1=1')
			->field('a.*,c.store_name')
			->order('a.add_time desc')
			->select();
        $this->assign('page', $page);
		$this->assign('withdraw', $withdraw);
		$this->display();
	}
}
